package cn.hospital.seviceimpl;

import cn.hospital.dao.*;
import cn.hospital.entity.*;
import cn.hospital.service.PatientInformationSheetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PatientInformationSheetServiceImpl implements PatientInformationSheetService {
    @Resource
    private PatientInformationSheetMapper patientInformationSheetMapper;
    @Resource
    private QqueryConditionsMapper qqueryConditionsMapper;
    @Resource
    private SignsOfBloodMapper signsOfBloodMapper;
    @Resource
    private AnemiaMapper anemiaMapper;
    @Resource
    private TemplateMapper templateMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private BoneMetastasesMapper boneMetastasesMapper;
    @Resource
    private BrainMetastasesMapper brainMetastasesMapper;
    @Resource
    private CertificateMapper certificateMapper;
    @Resource
    private DiagnosisMapper diagnosisMapper;
    @Resource
    private LiverMetastasisMapper liverMetastasisMapper;
    @Resource
    private LymphNodeMetastasisMapper lymphNodeMetastasisMapper;
    @Resource
    private MedicalInformationMapper medicalInformationMapper;
    @Resource
    private OperationInformationSheetMapper operationInformationSheetMapper;
    @Resource
    private OtherTransferMapper otherTransferMapper;
    @Resource
    private PatientsFamilyMapper patientsFamilyMapper;
    @Resource
    private PulmonaryMetastasisMapper pulmonaryMetastasisMapper;
    @Resource
    private ScoreMapper scoreMapper;
    @Resource
    private TumorMarkerMapper tumorMarkerMapper;
    @Resource
    private WardMapper wardMapper;
    @Resource
    private AnesthesiaInformationMapper anesthesiaInformationMapper;
    @Resource
    private ChestImagingMapper chestImagingMapper;
    @Resource
    private BloodTransfusionRecordMapper bloodTransfusionRecordMapper;

    /**
     * 根据条件查询病人数量
     * @param map
     * @return
     */
    public Long selectByMapCount(Map<String, Object> map, HttpSession session) {
        String[] query_field = (String[]) session.getAttribute("query_field");
        String[] query = (String[]) session.getAttribute("query");
        String[] query_conditions = (String[]) session.getAttribute("query_conditions");
        String[] scope_of = (String[]) session.getAttribute("scope_of");
        if (query_field != null) {
            for (int i = 0; i < query_field.length; i++) {
                //查询字段名称
                String query_field_name = query_field[i].substring(0, query_field[i].indexOf("-"));
                //查询方式
                String way = query_field_name + "_way";
                String query_name = query[i];
                //查询条件
                String query_conditions_name = query_conditions[i];
                map.put("" + query_field_name + "", query_conditions_name);
                map.put("" + way + "", query_name);
            }
        }
        return patientInformationSheetMapper.selectByMapCount(map);
    }

    /**
     * 根据条件，查询患者数据
     * @param map
     * @return
     */
    public List<PatientInformationSheet> selectByMap(Map<String,Object> map){
        return patientInformationSheetMapper.selectByMap(map);
    }

    /**
     * 根据条件,查询病人数量
     * @param map
     * @return
     */
    public Long selectByMapCount(Map<String, Object> map) {
        return patientInformationSheetMapper.selectByMapCount(map);
    }


    /**
     * 根据页面id查询病人数量，展示单个查询信息时
     *
     * @param queryConditionsId
     * @return
     */
    public Long selectByMapCount(String queryConditionsId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<QqueryConditions> QqueryConditionsList = qqueryConditionsMapper.selectByApplicationInformationId(queryConditionsId);
        if (QqueryConditionsList != null) {
            for (QqueryConditions QC : QqueryConditionsList) {
                //查询字段名称
                String query_field_name = QC.getQueryField().substring(0, QC.getQueryField().indexOf("-"));
                //查询方式
                String way = query_field_name + "_way";
                String query_name = QC.getQuery();
                //查询条件
                String query_conditions_name = QC.getQueryConditions();
                map.put("" + query_field_name + "", query_conditions_name);
                map.put("" + way + "", query_name);
            }
        }
        return patientInformationSheetMapper.selectByMapCount(map);
    }

    /**
     * 查询随访管理系统，查询单个患者信息
     * @param patientId
     * @return
     */
    @Override
    public List<PatientInformationSheet> selectPatientSignsAnemiaFoDiagnosis(Integer patientId) {
        return patientInformationSheetMapper.selectPatientSignsAnemiaFoDiagnosis(patientId);
    }

    /**
     * 患者详细页面中的保存按钮
     *
     * @param patientInformationSheet
     * @param record
     * @param anemia
     * @return
     */
    public Integer savePatientInformationSheet(PatientInformationSheet
                                                       patientInformationSheet, SignsOfBlood record, Anemia anemia) {
        Integer count = 0;
        count = updatePatientInformationSheet(patientInformationSheet);
        count = foininsertSelectiveSignsOfBlood(record);
        count = foininsertSelectiveAnemia(anemia);
        return 1;
    }


    /**
     * 修改病人信息
     *
     * @param patientInformationSheet
     * @return
     */
    public Integer updatePatientInformationSheet(PatientInformationSheet patientInformationSheet) {
        return patientInformationSheetMapper.updatePatientInformationSheet(patientInformationSheet);
    }


    /**
     * 根据科室查询模板
     *
     * @param session
     * @return
     */
    @Override
    public List<Template> selectTemplatem(HttpSession session) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        return templateMapper.selectTemplatemlist(meaningUser.getDepartmentId());
    }

    /**
     * 添加病人模板信息
     *
     * @param ward
     * @param patientInformationSheet
     * @param score
     * @param certificate
     * @param patientsFamily
     * @param diagnosis
     * @param operationInformationSheet
     * @param bloodTransfusionRecord
     * @param medicalInformation
     * @param signsOfBlood
     * @param anemia
     * @param chestImaging
     * @param medicalHistory
     * @param pulmonaryMetastasis
     * @param liverMetastasis
     * @param otherTransfer
     * @param lymphNodeMetastasis
     * @param brainMetastases
     * @param boneMetastases
     * @param tumorMarker
     * @return
     */
    public Integer insertTemplatemPatientInformationSheet(Ward ward, String provinceCode1, String cityCode1,
                                                          String countyCode1, String gates1, String provinceCode2, String cityCode2,
                                                          String countyCode2, String gates2,
                                                          PatientInformationSheet patientInformationSheet,
                                                          Score score, Certificate certificate, PatientsFamily patientsFamily,
                                                          Diagnosis diagnosis, OperationInformationSheet operationInformationSheet,
                                                          BloodTransfusionRecord bloodTransfusionRecord,
                                                          MedicalInformation medicalInformation, SignsOfBlood signsOfBlood,
                                                          Anemia anemia, ChestImaging chestImaging, MedicalHistory medicalHistory,
                                                          PulmonaryMetastasis pulmonaryMetastasis, AnesthesiaInformation anesthesiaInformation,
                                                          LiverMetastasis liverMetastasis,
                                                          OtherTransfer otherTransfer,
                                                          LymphNodeMetastasis lymphNodeMetastasis,
                                                          BrainMetastases brainMetastases,
                                                          BoneMetastases boneMetastases,
                                                          TumorMarker tumorMarker,HttpSession session) {
        Integer count = 0;
        /*首先判断患者信息是否已经存在医院数据库中*/
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("number", patientInformationSheet.getNumber());
        map.put("patientName", patientInformationSheet.getPatientName());
        PatientInformationSheet patientInformationSheet1 = selectByConditions(map);
        //如果(patientInformationSheet1)返回时null,则表示没有该患者信息
        if (patientInformationSheet1 == null) {
            if (ward != null && ward.equals("")) {
                insertSelective(ward);
                patientInformationSheet.setWardId(ward.getWardId());
            }
            if (provinceCode1 != null && provinceCode1.equals("")) {
                Address address1 = new Address();
                address1.setProvinceCode(provinceCode1);
                address1.setCityCode(cityCode1);
                address1.setCountyCode(countyCode1);
                address1.setGates(gates1);
                insert(address1);
                patientInformationSheet.setPlaceOfBirthId(address1.getAddressId());
            }
            if (provinceCode2 != null && provinceCode2.equals("")) {
                Address address2 = new Address();
                address2.setProvinceCode(provinceCode2);
                address2.setCityCode(cityCode2);
                address2.setCountyCode(countyCode2);
                address2.setGates(gates2);
                insert(address2);
                patientInformationSheet.setAddressId(address2.getAddressId());
            }
            insert(patientInformationSheet,session);
            if (boneMetastases != null && boneMetastases.equals("")) {
                boneMetastases.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(boneMetastases);
            }
            if (brainMetastases != null && brainMetastases.equals("")) {
                brainMetastases.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(brainMetastases);
            }
            if (certificate != null && certificate.equals("")) {
                certificate.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(certificate);
            }
            if (diagnosis != null && diagnosis.equals("")) {
                diagnosis.setPatientinformationsheetId(patientInformationSheet.getPatientId());
                insertSelective(diagnosis);
            }
            if (liverMetastasis != null && liverMetastasis.equals("")) {
                liverMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(liverMetastasis);
            }
            if (lymphNodeMetastasis != null && lymphNodeMetastasis.equals("")) {
                lymphNodeMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(lymphNodeMetastasis);
            }
            if (medicalInformation != null && medicalInformation.equals("")) {
                medicalInformation.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(medicalInformation);
            }
            if (operationInformationSheet != null && operationInformationSheet.equals("")) {
                operationInformationSheet.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(operationInformationSheet);
            }
            if (otherTransfer != null && otherTransfer.equals("")) {
                otherTransfer.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(otherTransfer);
            }
            if (patientsFamily != null && patientsFamily.equals("")) {
                patientsFamily.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(patientsFamily);
            }
            if (pulmonaryMetastasis != null && pulmonaryMetastasis.equals("")) {
                pulmonaryMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(pulmonaryMetastasis);
            }
            if (score != null && score.equals("")) {
                score.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(score);
            }
            if (tumorMarker != null && tumorMarker.equals("")) {
                tumorMarker.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(tumorMarker);
            }
            if (signsOfBlood != null && signsOfBlood.equals("")) {
                signsOfBlood.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(signsOfBlood);
            }
            if (chestImaging != null && chestImaging.equals("")) {
                chestImaging.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(chestImaging);
            }
            if (bloodTransfusionRecord != null && bloodTransfusionRecord.equals("")) {
                bloodTransfusionRecord.setPatientinformationsheetId(patientInformationSheet.getPatientId());
                insert(bloodTransfusionRecord);
            }
        } else {
            if (boneMetastases != null && boneMetastases.equals("")) {
                boneMetastases.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(boneMetastases);
            }
            if (brainMetastases != null && brainMetastases.equals("")) {
                brainMetastases.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(brainMetastases);
            }
            if (certificate != null && certificate.equals("")) {
                certificate.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(certificate);
            }
            if (diagnosis != null && diagnosis.equals("")) {
                diagnosis.setPatientinformationsheetId(patientInformationSheet.getPatientId());
                insertSelective(diagnosis);
            }
            if (liverMetastasis != null && liverMetastasis.equals("")) {
                liverMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(liverMetastasis);
            }
            if (lymphNodeMetastasis != null && lymphNodeMetastasis.equals("")) {
                lymphNodeMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(lymphNodeMetastasis);
            }
            if (medicalInformation != null && medicalInformation.equals("")) {
                medicalInformation.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(medicalInformation);
            }
            if (operationInformationSheet != null && operationInformationSheet.equals("")) {
                operationInformationSheet.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(operationInformationSheet);
            }
            if (otherTransfer != null && otherTransfer.equals("")) {
                otherTransfer.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(otherTransfer);
            }
            if (patientsFamily != null && patientsFamily.equals("")) {
                patientsFamily.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(patientsFamily);
            }
            if (pulmonaryMetastasis != null && pulmonaryMetastasis.equals("")) {
                pulmonaryMetastasis.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(pulmonaryMetastasis);
            }
            if (score != null && score.equals("")) {
                score.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(score);
            }
            if (tumorMarker != null && tumorMarker.equals("")) {
                tumorMarker.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(tumorMarker);
            }
            if (signsOfBlood != null && signsOfBlood.equals("")) {
                signsOfBlood.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(signsOfBlood);
            }
            if (chestImaging != null && chestImaging.equals("")) {
                chestImaging.setPatientId(patientInformationSheet.getPatientId());
                insertSelective(chestImaging);
            }
            if (bloodTransfusionRecord != null && bloodTransfusionRecord.equals("")) {
                bloodTransfusionRecord.setPatientinformationsheetId(patientInformationSheet.getPatientId());
                insert(bloodTransfusionRecord);
            }
        }
        return null;
    }

    /*---------------------------------------(患者信息添加对应的表)---------------------------------------------*/



    /**
     * 根据条件查询，该病人是否存在
     *
     * @param map（姓名，身份证号）
     * @return
     */
    public PatientInformationSheet selectByConditions(Map<String, Object> map) {
        return patientInformationSheetMapper.selectByConditions(map);
    }

    /**
     * 添加患者信息
     *
     * @param patientInformationSheet
     * @return
     */
    public Integer insert(PatientInformationSheet patientInformationSheet,HttpSession session) {
        MeaningUser meaningUser = (MeaningUser) session.getAttribute("meaningUser");
        patientInformationSheet.setTime(new Date());
        patientInformationSheet.setFoGroupState(2);
        patientInformationSheet.setDepartmentId( meaningUser.getDepartmentId() );
        return patientInformationSheetMapper.insert(patientInformationSheet);
    }

    /**
     * 添加地址信息
     *
     * @param address
     * @return
     */
    public Integer insert(Address address) {
        return addressMapper.insert(address);
    }

    /**
     * 病区添加表
     *
     * @param ward
     * @return
     */
    private Integer insertSelective(Ward ward) {
        return wardMapper.insertSelective(ward);
    }

    /**
     * 添加体格信息
     * @param record
     * @return
     */
    public int foininsertSelectiveSignsOfBlood(SignsOfBlood record) {
        record.setTime(new Date());
        return signsOfBloodMapper.insertSelective(record);
    }

    /**
     * 添加实验室检查
     *
     * @param record
     * @return
     */
    public int foininsertSelectiveAnemia(Anemia record) {
        record.setTime(new Date());
        return anemiaMapper.insertSelective(record);
    }

    /**
     * 骨转移
     *
     * @param boneMetastases
     * @return
     */
    public int insertSelective(BoneMetastases boneMetastases) {
        boneMetastases.setDiagnosisTime(new Date());
        return boneMetastasesMapper.insertSelective(boneMetastases);
    }

    /**
     * 脑转移表添加
     *
     * @param brainMetastases
     * @return
     */
    public int insertSelective(BrainMetastases brainMetastases) {
        brainMetastases.setDiagnosisTime(new Date());
        return brainMetastasesMapper.insertSelective(brainMetastases);
    }

    /**
     * 证件表的添加
     *
     * @param certificate
     * @return
     */
    public int insertSelective(Certificate certificate) {
        return certificateMapper.insertSelective(certificate);
    }

    /**
     * 诊断表添加
     *
     * @param diagnosis
     * @return
     */
    public int insertSelective(Diagnosis diagnosis) {
        return diagnosisMapper.insertSelective(diagnosis);
    }

    /**
     * 肝转移表添加
     *
     * @param iverMetastasis
     * @return
     */
    public int insertSelective(LiverMetastasis iverMetastasis) {
        iverMetastasis.setDiagnosisTime(new Date());
        return liverMetastasisMapper.insertSelective(iverMetastasis);
    }

    /**
     * 淋巴结转移
     *
     * @param lymphNodeMetastasis
     * @return
     */
    public int insertSelective(LymphNodeMetastasis lymphNodeMetastasis) {
        lymphNodeMetastasis.setDiagnosisTime(new Date());
        return lymphNodeMetastasisMapper.insertSelective(lymphNodeMetastasis);
    }

    /**
     * 用药信息表
     *
     * @param medicalInformation
     * @return
     */
    public int insertSelective(MedicalInformation medicalInformation) {
        medicalInformation.setDoctorsAdviceTime(new Date());
        return medicalInformationMapper.insertSelective(medicalInformation);
    }

    /**
     * 手术表添加
     *
     * @param operationInformationSheet
     * @return
     */
    public int insertSelective(OperationInformationSheet operationInformationSheet) {
        return operationInformationSheetMapper.insertSelective(operationInformationSheet);
    }

    /**
     * 其他转移表添加
     *
     * @param otherTransfer
     * @return
     */
    public int insertSelective(OtherTransfer otherTransfer) {
        otherTransfer.setDiagnosisTime(new Date());
        return otherTransferMapper.insertSelective(otherTransfer);
    }

    /**
     * 病人家属表添加
     *
     * @param patientsFamily
     * @return
     */
    public int insertSelective(PatientsFamily patientsFamily) {
        return patientsFamilyMapper.insertSelective(patientsFamily);
    }

    /**
     * 肺转移表添加
     *
     * @param pulmonaryMetastasis
     * @return
     */
    public int insertSelective(PulmonaryMetastasis pulmonaryMetastasis) {
        pulmonaryMetastasis.setDiagnosisTime(new Date());
        return pulmonaryMetastasisMapper.insertSelective(pulmonaryMetastasis);
    }

    /**
     * 评分表的添加
     *
     * @param score
     * @return
     */
    public int insertSelective(Score score) {
        return scoreMapper.insertSelective(score);
    }

    /**
     * 肿瘤表添加
     *
     * @param tumorMarker
     * @return
     */
    int insertSelective(TumorMarker tumorMarker) {
        return tumorMarkerMapper.insertSelective(tumorMarker);
    }

    /**
     * 添加麻醉信息
     *
     * @param record
     * @return
     */
    public int insertSelective(AnesthesiaInformation record) {
        return anesthesiaInformationMapper.insertSelective(record);
    }

    /**
     * 添加体格检查信息
     *
     * @param record
     * @return
     */
    public int insertSelective(SignsOfBlood record) {
        record.setTime(new Date());
        return signsOfBloodMapper.insertSelective(record);
    }

    /**
     * 添加方法
     *
     * @param record
     * @return
     */
    public int insertSelective(ChestImaging record) {
        return chestImagingMapper.insertSelective(record);
    }

    /**
     * 添加信息
     *
     * @param record
     * @return
     */
    public int insert(BloodTransfusionRecord record) {
        record.setBloodTransfusionTime(new Date());
        return bloodTransfusionRecordMapper.insert(record);
    }
}
